package com.bjpowernode.crm.settings.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.MD5Util;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : UserController
 * @Author : 司猫子
 * @Date: 2020/12/23 12:52
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/settings/qx/user/toLogin.do")
    public String toLogin(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String loginAct = null;
        String loginPwd = null;

        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if("loginAct".equals(name)){
                loginAct = cookie.getValue();
            }
            if("loginPwd".equals(name)){
                loginPwd = cookie.getValue();
            }

        }
        if(loginAct != null && loginPwd != null){
            HashMap<String,Object> map = new HashMap<String, Object>();
            map.put("loginAct", loginAct);
            map.put("loginPwd", MD5Util.getMD5(loginPwd));
            User user = userService.selectUserByLoginActAndPwd(map);
            request.getSession().setAttribute(Contants.SUSSION_USER, user);
            return "redirect:/workbench/index.do";
        }else {
            return "settings/qx/user/login";
        }
    }
    @RequestMapping("/settings/qx/user/login.do")
    @ResponseBody
    public Object login(String loginAct, String loginPwd, String isRemPwd, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println("拿到了登陆的参数");
        String remoteAddr = request.getRemoteAddr();
        System.out.println("!!!!!!!!!!remoteAddr="+remoteAddr);
        System.out.println(loginAct);
        System.out.println(loginPwd);
        System.out.println(isRemPwd);
        String newloginPwd = MD5Util.getMD5(loginPwd);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",newloginPwd);
        User user = userService.selectUserByLoginActAndPwd(map);
        ReturnObject returnObject = new ReturnObject();
        if(user == null){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("用户名或密码错误");
        }else {
            if (DateUtils.formatDateTime(new Date()).compareTo(user.getExpireTime()) > 0) {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("账号时间已过期");
            }else if("0".equals(user.getLockState())) {
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("账号已经被锁定");
            }
//            else if(!user.getAllowIps().contains(request.getRemoteAddr())){
//                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
//                returnObject.setMessage("IP受限");
//                System.out.println(request.getRemoteAddr());
//            }
            else {

                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                session.setAttribute(Contants.SUSSION_USER, user);
                if("true".equals(isRemPwd)){
                    Cookie c1 = new Cookie("loginAct", loginAct);
                    c1.setMaxAge(10*24*60*60);
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd", loginPwd);
                    c2.setMaxAge(10*24*60*60);
                    response.addCookie(c2);
                }else {
                    Cookie c1 = new Cookie("loginAct", null);
                    c1.setMaxAge(0);
                    response.addCookie(c1);
                    Cookie c2 = new Cookie("loginPwd", null);
                    c2.setMaxAge(0);
                    response.addCookie(c2);
                }
            }
        }
        return returnObject;
    }
    @RequestMapping("/settings/qx/user/logout.do")
    public String logout(HttpServletResponse response,HttpSession session){
        Cookie c1 = new Cookie("loginAct", null);
        c1.setMaxAge(0);
        response.addCookie(c1);
        Cookie c2 = new Cookie("loginPwd", null);
        c2.setMaxAge(0);
        response.addCookie(c2);
        session.invalidate();
        return "redirect:/";
    }


}
